package models;

import play.*;
import play.data.validation.Required;
import play.db.jpa.*;
import tools.StringUtil;
import validation.Unique;

import javax.persistence.*;

import java.io.UnsupportedEncodingException;
import java.util.*;
/**
 * 版块：文章是按照板块来进行划分的。
 * @author yujie
 */
@Entity
public class BanKuai extends ShiTi implements Comparable {
	/**
	 * 编号：同级板块按编号从小到大排序
	 */
	public int bianHao;
	/**
	 * 上级版块（非空）
	 * 双向映射关系：多个下级版块对应一个上级版块
	 */
	@Required
	@ManyToOne
    public BanKuai shangJiBK;
	
	/**
	 * 下级版块
	 * 双向映射关系：一个版块对应多个下级版块
	 */
    @OneToMany(cascade = CascadeType.ALL,mappedBy="shangJiBK")
    public Set<BanKuai> xiaJiBKs = new TreeSet();
    
    /**
     * 名称（非空，唯一）
     */
    @Required
    @Unique(message = "BanKuai.mingCheng.Unique")
    public String mingCheng;
    
    /**
     * 文章集合
     * 双向映射关系：一个版块对应多个文章
     */
    @OneToMany(mappedBy="banKuai")
    public Set<WenZhang> wenZhangs = new TreeSet();
    
    
    public BanKuai(int bianHao, String mingCheng) {
		super();
		this.bianHao = bianHao;
		this.mingCheng = mingCheng;
	}
    /**
     * 添加下级版块
     * @param banKuai
     */
    public void sheXiaJiBK(BanKuai banKuai){
    	if(banKuai.shangJiBK!=null&&banKuai.shangJiBK.id!=this.id){
    		banKuai.shangJiBK.xiaJiBKs.remove(banKuai);
    		banKuai.shangJiBK.save();
    	}
    	banKuai.shangJiBK=this;
    	if(!this.xiaJiBKs.contains(banKuai)){
    		this.xiaJiBKs.add(banKuai);
    	}
    }
    /**
     * 重新设定上级版块，并维护双向映射关系
     * @param banKuai
     */
    public void sheShangJiBK(BanKuai banKuai){
    	if(this.shangJiBK!=null&&this.shangJiBK.id!=banKuai.id){
    		this.shangJiBK.xiaJiBKs.remove(this);
    	}
    	this.shangJiBK=banKuai;
    	if(!banKuai.xiaJiBKs.contains(this)){
    		banKuai.xiaJiBKs.add(this);
    	}
    }
    /**
     * 导航：递归生成从最高级版块到给定版块的路径链表（如：首页 - 一级版块 - 二级版块）
     * @param banKuai
     * @param banKuais
     */
	public static void daoHang(BanKuai banKuai,List<BanKuai> banKuais){
    	banKuais.add(0, banKuai);
    	if(banKuai.shangJiBK==null)
    		return;
    	else
    		daoHang(banKuai.shangJiBK,banKuais);
    }
	/**
	 * 根据给定的版块名称获取版块对象
	 * @param banKuaiMing
	 * @return
	 */
    public static BanKuai mingCheng2BanKuai(String banKuaiMingCheng){
    	return BanKuai.find("select bk from BanKuai as bk where mingCheng='"+banKuaiMingCheng+"'").first();
    }
    /**
     * 根据指定页和每页大小（文章数量）来获取文章对象链表（按照发布时间降序排列）
     * @param ye
     * @param yeDaXiao
     * @return
     */
    public List<WenZhang> zuiJinWenZhang(int ye,int yeDaXiao){
    	return WenZhang.find("banKuai = ? order by xinZengShiJian desc", this).fetch(ye, yeDaXiao);
    }
    /**
     * 根据给定的版块名称获取该版块对象的下级版块集合
     * @param banKuaiMingCheng
     * @return
     */
    public static Set<BanKuai> xiaJiBanKuais(String banKuaiMingCheng){
    	return mingCheng2BanKuai(banKuaiMingCheng).xiaJiBKs;
    }
	@Override
	public String toString() {
		if(shangJiBK!=null)
			return shangJiBK+"-"+mingCheng;
		else return mingCheng;
	}
	/**
	 * 根据编号大小进行比较
	 */
	@Override
	public int compareTo(Object o) {
		int bianHao2=((BanKuai)o).bianHao;
		return bianHao-bianHao2;
	}
    
}
